<?php


namespace App\Http\Controllers\Admin;


use App\Model\Coupon;
use App\Model\CouponGoodsCategory;
use App\Model\CouponGoodsSku;
use App\Model\CouponRule;
use App\Tool\Json;

class CouponController extends AuthCheckController
{

    /**
     * 优惠券添加和修改
     * Create by PeterYang
     * 2020/07/19 10:31:10
     * Email:904801074@qq.com
     * @return false|string
     */
    function coupon_update()
    {


        try {


            \DB::beginTransaction();

            $post = request()->input();

            $id = $post['id'];

            $couponRuleData=[
                'limit' => $post['limit'],
                'expiration_date_type' => $post['expiration_date_type'],
                'start_time' => $post['start_time'],
                'end_time' => $post['end_time'],
                'until_days' => $post['until_days'],
                'expiration_days' => $post['expiration_days'],
                'goods_type' => $post['goods_type'],
                'get_expired_time'=>$post['get_expired_time']?:null
            ];



            if ($id) {

                //写入优惠券规则表
                $couponRuleInfo = CouponRule::find($id)->fill($couponRuleData)->save();


                //固定商品列表

                CouponGoodsSku::where('coupon_rule_id',$id)->delete();

                foreach ($post['goods_list'] as $key => $value) {


                    CouponGoodsSku::create([
                        'coupon_rule_id'=>$id,
                        'goods_id'=>$value['goods_id'],
                        'goods_sku_id'=>$value['id']
                    ]);

                }

                //固定分类列表

                CouponGoodsCategory::where('coupon_rule_id',$id)->delete();

                foreach ($post['category_list'] as $key => $value) {

                    CouponGoodsCategory::create([
                        'coupon_rule_id'=>$id,
                        'category_id'=>$value['id'],

                    ]);

                }

                //优惠券规则

                Coupon::where('coupon_rule_id',$id)->delete();

                foreach ($post['coupon_list'] as $key=>$value){

                    Coupon::create([
                        'name'=>$value['name'],
                        'num'=>$value['num'],
                        'type'=>$value['type'],
                        'money'=>$value['money'],
                        'discount'=>$value['discount'],
                        'condition'=>$value['condition'],
                        'coupon_rule_id'=>$id,
                        'condition_money'=>$value['condition_money']
                    ]);

                }






            } else {


                $couponRuleInfo = CouponRule::create($couponRuleData);


                $couponRuleId = $couponRuleInfo->id;

                foreach ($post['goods_list'] as $key => $value) {


                    CouponGoodsSku::create([
                        'coupon_rule_id'=>$couponRuleId,
                        'goods_id'=>$value['goods_id'],
                        'goods_sku_id'=>$value['id']
                    ]);

                }


                foreach ($post['category_list'] as $key => $value) {


                    CouponGoodsCategory::create([
                        'coupon_rule_id'=>$couponRuleId,
                        'category_id'=>$value['id'],

                    ]);

                }

                foreach ($post['coupon_list'] as $key=>$value){

                    Coupon::create([
                        'name'=>$value['name'],
                        'num'=>$value['num'],
                        'type'=>$value['type'],
                        'money'=>$value['money'],
                        'discount'=>$value['discount'],
                        'condition'=>$value['condition'],
                        'coupon_rule_id'=>$couponRuleId,
                        'condition_money'=>$value['condition_money']
                    ]);

                }



            }



            \DB::commit();
            return Json::code(1, 'success', request()->input());


        } catch (\Exception $exception) {

            \DB::rollBack();

        }
    }

    function coupon_list(){

        $list=CouponRule::with('coupon')->orderBy('id','desc');




        return Json::code(1,'success',paginate($list,request()->input('p',1)));
    }


    function coupon_detail(){



        $id=request()->input('id');

        $data=CouponRule::with('goods_list.goods')
            ->with('goods_list.goods_sku_detail')
            ->with('coupon_list')
            ->with('category_list')
            ->find($id);


//        return json_encode(['code'=>1,'msg'=>'success','data'=>$data]);

        return Json::code(1,'success',$data);
    }


    function coupon_delete(){


        $id=request()->input('id');

        $id=(int)$id;

        try {

            \DB::beginTransaction();

            CouponRule::destroy($id);

            Coupon::where('coupon_rule_id',$id)->delete();

            CouponGoodsSku::where('coupon_rule_id',$id)->delete();

            CouponGoodsCategory::where('coupon_rule_id',$id)->delete();


            \DB::commit();

            return Json::code(1,'success');

        }catch (\Exception $exception){

            \DB::rollBack();

            return Json::code(2,$exception->getMessage());
        }



    }

}
